import 'dart:convert';
import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';

//简单粗暴方式处理校验证书方法
http.Client sslClient() {
  var ioClient = HttpClient()
    ..badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
  http.Client _client = IOClient(ioClient);
  return _client;
}

Future<PageData> loadPageConfig() async {
  final response = await sslClient().get(Uri.parse(
      "https://819d.goodswitchs.com/1739911732.png?D39SdQM9VzMII1I7UjUHYlN7=="));
  if (response.statusCode == 200) {
    final body = jsonDecode(response.body) as Map<String, dynamic>;
    print("body:$body");
    if (body["code"] == 200 && body["data"] != null) {
      return PageData.create(true, body["data"]);
    }
  }
  return PageData(isEnable: false, aa: "", bb: "", cc: 1, dd: "");
}

class PageData {
  final bool isEnable;
  final String aa;
  final String bb;
  final int cc;
  final String dd;

  PageData(
      {required this.isEnable, required this.aa, required this.bb, required this.cc, required this.dd});

  Color toColor() {
    if (bb == null || bb.isEmpty) return Colors.white;
    String colorStr = bb.replaceFirst("#", "");
    int hexColor = int.parse("0xFF" + colorStr);
    return Color(hexColor);
  }

  List<String> parserKeyword() {
    return dd.split(",");
  }

  factory PageData.create(bool isEnable, Map<String, dynamic> json) {
    return PageData(isEnable: isEnable, aa: json["aa"],
        bb: json["bb"], cc: json["cc"], dd: json["dd"]);
  }
}
